package com.dfbz.bigdata.modules.system.controller;

import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.dfbz.bigdata.common.utils.R;
import com.dfbz.bigdata.modules.system.doman.CommonGoods;

import com.dfbz.bigdata.modules.system.dto.CommonGoodsDto;
import com.dfbz.bigdata.modules.system.service.CommonGoodsService;
import lombok.Getter;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import java.util.Date;
import java.util.List;

/**
 * @Auther:东方标准
 * @Date:2022/9/26-09-26-16:48
 * @Decsription:com.dfbz.bigdata.modules.system.controller
 * @Version:1.0
 */
@RestController
@RequestMapping("/goods")
@Slf4j
public class CommonGoodsController {

    @Autowired
    private CommonGoodsService commonGoodsService;

    @GetMapping
    public R<Page> getgoodslist(String goodName, Long currentPage, Long pagesizes, String order, String sort) {
        log.info("{}", goodName);
        Page page = new Page(currentPage, pagesizes);

        LambdaQueryWrapper<CommonGoods> lqw = new LambdaQueryWrapper<>();
        lqw.like(!goodName.isEmpty(), CommonGoods::getGoodName, goodName);
        lqw.or().like(!goodName.isEmpty(), CommonGoods::getGoodDescription, goodName);
        if (sort != null) {
            if (sort.equals("price")) {
                if (order != null) {
                    if (order.equals("asc")) {
                        lqw.orderByAsc(CommonGoods::getGoodPrice);
                    } else if (order.equals("desc")) {
                        lqw.orderByDesc(CommonGoods::getGoodPrice);
                    }
                }
            }
        }


        commonGoodsService.page(page, lqw);

        return R.success(page);
    }


    @GetMapping("/{id}")
    public R<CommonGoods> getByIdGoods(@PathVariable Long id) {
        log.info("id------{}",id);

        CommonGoods commonGoods = commonGoodsService.getById(id);
        System.out.println(commonGoods);
        return R.success(commonGoods);
    }


    /**
     * 不穿参数查全部  传一个查子分类  传两个查孙分类
     *
     * @param parentId
     * @param category
     * @return
     */
    @GetMapping("/miniapp/list")
    public R<List<CommonGoodsDto>> getminiapplist(Integer parentId, Integer category) {

        log.info("---{}---{}", parentId, category);
        List<CommonGoodsDto> list = commonGoodsService.getList(parentId, category);

//        System.out.println(list);
        return R.success(list);

//        return "test";

    }

    //    小程序商品 详细页
    @GetMapping("/miniapp")
    public R<CommonGoods> getMinByIdGoods(Long id) {
        log.info("-------------------miniappID{}--------------", id);
        CommonGoods commonGoods = commonGoodsService.getById(id);
        System.out.println(commonGoods);
        return R.success(commonGoods);
    }

    @DeleteMapping("/{id}")
    public R<String> delete(@PathVariable Long id) {

        boolean b = commonGoodsService.removeById(id);
        if (b) {
            return R.success("删除成功^_^!!!!!");
        } else return R.error("系统繁忙-_-!!");

    }

    @PutMapping("update")
    public R<String> updateGoods(@RequestBody CommonGoods commonGoods) {

        log.info("更新----------------{}", commonGoods);
        boolean b = commonGoodsService.updateById(commonGoods);
        if (b) {
            return R.success("更新成功^_^!!!!!");
        }
        return R.error("服务器繁忙-_-!!");

    }

    @PostMapping("insert")
    public R<String> insert(@RequestBody CommonGoods commonGoods) {
        log.info("新增----------------{}", commonGoods);
        commonGoods.setCreateTime(new Date());
        commonGoods.setUpdateTime(new Date());
        boolean b = commonGoodsService.save(commonGoods);
        if (b) {
            return R.success("新增成功^_^!!!!!");
        }
        return R.success("服务器繁忙-_-!!");

    }
    @GetMapping("/goodId")
    public R<CommonGoods> getGoodId(Long goodId) {
        LambdaQueryWrapper<CommonGoods> lqw=new LambdaQueryWrapper<>();
        lqw.eq(CommonGoods::getGoodId, goodId);
        CommonGoods Goodone = commonGoodsService.getOne(lqw);
        return R.success(Goodone);
    }
}
